package com.zcgame.loadimagedemo

import android.Manifest
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.zcgame.loadimagedemo.utils.IOUtils
import com.zcgame.loadimagedemo.utils.PermissionUtils
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class MainActivity : AppCompatActivity() {
    private val url = "https://cdn.kepan365.com/dev/NiJB7EhkWZz5HJfr7RxP4x8ePSwwpGTz.jpg"
    private val PERMISSIONS = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        Glide.with(this).load(url).into(main_image)
        //下载图片保存至文件
        downloadImage1()
        //下载图片保存bitmap
        downloadImage2()
    }

    private fun downloadImage2() {
        val dest = File(cacheDir, "image${System.currentTimeMillis()}.jpg")
        Log.d("HuangLJ",dest.absolutePath)
        Glide.with(this).asBitmap().load(url).addListener(object :RequestListener<Bitmap>{
            override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                return false
            }

            override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                IOUtils.saveImage2Gallery(this@MainActivity,resource!!,dest)
                return false
            }
        }).preload()
    }

    /**
     * 先下载下来
     */
    private fun downloadImage1() {
        val dest = File(cacheDir, "image${System.currentTimeMillis()}.jpg")
        Log.d("HuangLJ",dest.absolutePath)
        lifecycleScope.launch(Dispatchers.Main){
            Glide.with(this@MainActivity).downloadOnly().load(url).addListener(object :RequestListener<File>{
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<File>?,
                    isFirstResource: Boolean
                ): Boolean {
                    Log.d("HuangLJ","downloadFailed")
                    return false
                }

                override fun onResourceReady(
                    resource: File?,
                    model: Any?,
                    target: Target<File>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {

                    lifecycleScope.launch(Dispatchers.IO){
                        IOUtils.copyFileUsingChannels(resource!!.absoluteFile,dest)
                        IOUtils.scannerImage2(this@MainActivity,dest)
                    }
                    return false
                }
            }).preload()
        }

        PermissionUtils(this).need(PERMISSIONS).subscribe(object :
            PermissionUtils.Subscribe{
            override fun onResult(allGranted: Boolean) {
                if(allGranted){
                    Log.d("HuangLJ","permission request success")
                }else{
                    Log.d("HuangLJ","permission request failed")
                }
            }
        })
    }
}